package com.xuecheng.media.domain.model.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import java.time.LocalDateTime;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import java.io.Serializable;

import com.xuecheng.media.infrastructure.enums.MediaProcessStatusEnum;
import lombok.Data;
import com.baomidou.mybatisplus.annotation.TableName;

/**
 * <p>
 * 
 * </p>
 *
 * @author itcast
 */
@Data
@TableName("media_process")
public class MediaProcess implements Serializable {

    private static final long serialVersionUID = 1L;

    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    /**
     * 文件标识
     */
    private String fileId;

    /**
     * 文件名称
     */
    private String filename;

    /**
     * 存储桶
     */
    private String bucket;

    /**
     * 存储路径
     */
    private String filePath;

    /**
     * 状态,1:未处理，2：处理成功  3处理失败
     */
    private String status;

    /**
     * 上传时间
     */
    @TableField(fill = FieldFill.INSERT)
    private LocalDateTime createDate;

    /**
     * 完成时间
     */
    @TableField("finish_date")
    private LocalDateTime finishDate;

    /**
     * 媒资文件访问地址
     */
    private String url;

    /**
     * 失败原因
     */
    @TableField("errormsg")
    private String errormsg;

    /**
     * 失败次数
     */
    @TableField("fail_count")
    private Integer failCount;

    /**
     * 执行时间
     */
    @TableField("execute_date")
    private LocalDateTime executeDate;

    public static MediaProcess buildByMediaFiles(MediaFiles mediaFiles) {
        MediaProcess mediaProcess = new MediaProcess();
        mediaProcess.setFileId(mediaFiles.getFileId());
        mediaProcess.setFilename(mediaFiles.getFilename());
        mediaProcess.setBucket(mediaFiles.getBucket());
        mediaProcess.setFilePath(mediaFiles.getFilePath());
        mediaProcess.setStatus(MediaProcessStatusEnum.DEFAULT.getType());
        mediaProcess.setCreateDate(mediaFiles.getCreateDate());
        mediaProcess.setUrl(mediaFiles.getUrl());
        mediaProcess.setFailCount(0);
        return mediaProcess;
    }

    public MediaProcess buildError(String msg) {
        this.setStatus(MediaProcessStatusEnum.ERROR.getType());
        this.setErrormsg(this.errormsg + "," + msg);
        this.setFailCount(this.failCount + 1);
        return this;
    }

    public MediaProcess buildSuccess() {
        this.setStatus(MediaProcessStatusEnum.SUCCESS.getType());
        this.setFinishDate(LocalDateTime.now());
        return this;
    }
}
